//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __SVROBJ_H__
#define __SVROBJ_H__

#include <elasys_server.h>
#include <linknode.h>
#include <aura.h>

#define CFG_OID2OBJ_HS      16
#define CFG_NAME2NAMEHOOK_HS 16

#define HASHCODE(x) (x & (CFG_OID2OBJ_HS - 1))

class Object;

class NameHook : public DLinkNode
{
public:
    ProcessId m_processId;  // The Id of process that registered the name service.
    Object  * m_pObject;
    WStringBuf * m_pName;

public:
    NameHook();
    ~NameHook();
    ECode Initialize(
        /* [in] */ ProcessId processId,
        /* [in] */ Object * pObject,
        /* [in] */ WString name);

public:
    STATIC DLinkNode  s_namedHash[CFG_NAME2NAMEHOOK_HS];
    STATIC PVoid      s_hHashLock;

public:
    STATIC ECode S_Initialize();

    STATIC void S_Destroy();

    STATIC NameHook * S_Create(
        /* [in] */ Object * pObject,
        /* [in] */ ProcessId processId,
        /* [in] */ WString name);

    STATIC NameHook * S_Find(
        /* [in] */ WString name);

    STATIC void S_Dump();

    STATIC void S_Delete(
        /* [in] */ NameHook * pNameHook);

    STATIC void S_Delete(
        /* [in] */ ProcessId processId);

    STATIC ECode S_DeleteAndRelease(
        /* [in] */ NameHook * pNameHook);
};

INLINE NameHook::NameHook()
{
    m_processId = AURA_INVALID_PID;
    m_pObject = NULL;
    m_pName   = NULL;
}

class Object: public SLinkNode
{
public:
    void Initialize(
        /* [in] */ ProcessId processId,
        /* [in] */ ObjectType objType,
        /* [in] */ NameHook * pNameHook);

    void Initialize(
        /* [in] */ ProcessId processId,
        /* [in] */ PEMuid pClsId,
        /* [in] */ UInt32 uIndex,
        /* [in] */ NameHook * pNameHook);

public:
    ProcessId  m_processId;
    ObjectType m_objectType;
    oid_t        m_oid;

    NameHook * m_pNameHook;

    EMuid      m_clsId;
    UInt32     m_uIndex;

// static members.
public:
    STATIC SLinkNode s_hashObject[CFG_OID2OBJ_HS];
    STATIC PVoid     s_hHashLock;
    STATIC Int32     s_curOid;

public:
    STATIC ECode S_Initialize();

    STATIC void  S_Destroy();

    STATIC oid_t   S_GenerateOid();

    STATIC Object * S_GetObject(
        /* [in] */ oid_t oid);

    STATIC void S_AddObject(
        /* [in] */ Object * pObject);

    STATIC NameHook * S_DeleteObject(
        /* [in] */ oid_t oid);

    STATIC ECode S_DeleteObject(
        /* [in] */ ProcessId processId,
        /* [in] */ oid_t oid);

    STATIC ECode S_DeleteObjects(
        /* [in] */ ProcessId processId);
};

#endif //__SVROBJ_H__

